package mc0101.mccache.config;

import org.springframework.beans.factory.annotation.Value;
import mc0101.mccache.cache.LRULinkedHashMap;
import mc0101.mccache.cache.TwoLevelLocalCache;
import mc0101.mccache.util.CacheInvalidJudgeUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnBean(CacheInvalidJudgeUtils.class)
public class TwoLevelLocalCacheConfig {

    @Value("${cache.twoLevelLocalCache.cacheCapacity:2048}")
    private int cacheCapacity;

    @Value("${cache.twoLevelLocalCache.switch:true}")
    private boolean twoLevelLocalCacheSwitch;

    @Bean
    public TwoLevelLocalCache twoLevelLocalCache(){
        TwoLevelLocalCache twoLevelLocalCache = new TwoLevelLocalCache();
        twoLevelLocalCache.setTwoLevelLocalCacheSwitch(twoLevelLocalCacheSwitch);
        twoLevelLocalCache.setCacheCapacity(cacheCapacity);
        if (twoLevelLocalCacheSwitch){
            twoLevelLocalCache.setCache(new LRULinkedHashMap<>(cacheCapacity));
        }
        return twoLevelLocalCache;
    }
}